package service.impl;

import java.util.ArrayList;
import java.util.List;
import persistence.dao.CityDao;
import persistence.dao.DaoManager;
import persistence.dao.base.ConnectionBehavior;
import persistence.dao.base.DaoCommand;
import persistence.entity.CityEntity;
import model.City;
import model.ICity;
import service.CityService;

public class CityServiceImpl implements CityService {

	private DaoManager daoManager;
	
	public CityServiceImpl(DaoManager daoManager) {
		this.daoManager = daoManager;
	}
	
	protected DaoManager getDaoManager() {
		return daoManager;
	}
	
	@Override
	public ICity setCity(final String name) {
		return (ICity)
		getDaoManager().command(new DaoCommand(){
	      public Object execute(final DaoManager manager) {
		    	  final CityDao cityDao = manager.getDao(CityDao.class);
		    	  cityDao.setCity(name);
		    	  return null;  
		      }
	    }, ConnectionBehavior.CLOSE);
	}
	
	@Override
	public ICity setCityUpdate(final int[] id_city, final String[] name) {
		return (ICity)
		getDaoManager().command(new DaoCommand(){
	      public Object execute(final DaoManager manager) {
		    	  final CityDao cityDao = manager.getDao(CityDao.class);
		    	  cityDao.setCityUpdate(id_city,name);
		    	  return null; 
		      }
	    }, ConnectionBehavior.CLOSE);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ICity> getCity() {
		return (List<ICity>)
		getDaoManager().command(new DaoCommand(){
	      public Object execute(final DaoManager manager) {
	    	  final CityDao cityDao = manager.getDao(CityDao.class);
	    	  final List<CityEntity> listCityEntity = cityDao.getCity();
	    	  final List<ICity> listCity = new ArrayList<ICity>();
	    	  for (final CityEntity cityEntity : listCityEntity) {
	    		  listCity.add(convertToModel(cityEntity));
	    	  }
	    	  return listCity;
	      }
	    }, ConnectionBehavior.CLOSE);
	}
	
	public List<ICity> getCityById(final int[] id_city) {
		return (List<ICity>)
		getDaoManager().command(new DaoCommand(){
	      public Object execute(final DaoManager manager) {
	    	  final CityDao cityDao = manager.getDao(CityDao.class);
	    	  final List<CityEntity> listCityEntity = cityDao.getCityById(id_city);
		  		
	    	  final List<ICity> listCity = new ArrayList<ICity>();
	    	  for (final CityEntity cityEntity : listCityEntity) {
	    		  listCity.add(convertToModel(cityEntity));
	    	  }
	    	  return listCity;
	      }
	    }, ConnectionBehavior.CLOSE);
	}
	
	@Override
	public ICity getCity(final String name) {
		return (ICity) 
				getDaoManager().command(new DaoCommand(){
			      public Object execute(final DaoManager manager) {
			    	  final CityDao cityDao = manager.getDao(CityDao.class);
			    	  return convertToModel(cityDao.getCity(name));
			      }
			    }, ConnectionBehavior.CLOSE);
	}
	
	@Override
	public ICity delete(final int[] id_city) {
		return (ICity)
		getDaoManager().command(new DaoCommand(){
	      public Object execute(final DaoManager manager) {
		    	  final CityDao cityDao = manager.getDao(CityDao.class);
		    	  cityDao.delete(id_city);
		    	  return null; 
		      }
	    }, ConnectionBehavior.CLOSE);
	}
	
	private ICity convertToModel(CityEntity cityEntity) {
		ICity city = null;
		if (cityEntity != null) {
			city = new City(
				cityEntity.getId_city(),
				cityEntity.getName_city());
		}
		return city;
	}
}
